package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Administrator;
import net.suncaper.demo.domain.Student;
import net.suncaper.demo.domain.Teacher;
import net.suncaper.demo.service.LoginService;
import org.apache.ibatis.annotations.Param;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private LoginService loginService;
    public String UserId;
    @GetMapping("/login")
    public String login(Model model) {
//        model.addAttribute("user", new Administrator());
        return "/pages/user/login.html";
    }

    @PostMapping("/login")
    public String savelogin(Model mode,@RequestParam String id, @RequestParam String password, @RequestParam int select) {
        if (select == 1) {
            if (loginService.ADverify(id,password)) {
                return "redirect:/information/set?UserId="+id+"&select=1";
            } else {
                return "/pages/user/loginfail.html";
            }
        }
        else if (select == 2){
            if (loginService.TEverify(id,password)) {
                return "redirect:/information/set?UserId="+id+"&select=2";
            } else {
                return "/pages/user/loginfail.html";
            }
        }
        else if (select == 3) {
            if (loginService.STverify(id,password)) {
                return "redirect:/information/set?UserId="+id+"&select=3";
            } else {
                return "/pages/user/loginfail.html";
            }
        }
        else {
            return "redirect:/user/loginfail";
        }
    }


}